﻿using System;
using System.Collections.Generic;
using System.Web.Routing;
using System.Web.Mvc;
using Microsoft.Practices.Unity;
using Vit.Mvc.Controllers;

namespace Vit.Mvc
{
    public class UnityControllerFactory : DefaultControllerFactory
    {
        private readonly IUnityContainer unity;

        public UnityControllerFactory(IUnityContainer unity)
        {
            this.unity = unity;
        }

        protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
        {
            if (controllerType != null && typeof(Controller).IsAssignableFrom(controllerType))
            {
                Controller controller = unity.Resolve(controllerType) as Controller;

                if (controller != null)
                    controller.ActionInvoker = unity.Resolve<IActionInvoker>();

                return controller;
            }
            return null;
        }
    }
}
